home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / System / ReqToolsLib / Source / reqtools / boopsi.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-02  |  6.5 KB  |  288 lines

  1. #include <graphics/gfxbase.h>
  2. #include <graphics/rastport.h>
  3. #include <graphics/text.h>
  4. #include <intuition/intuition.h>
  5. #include <intuition/imageclass.h>
  6. #include <intuition/intuition.h>
  7. #ifdef _AROS
  8. #include <aros/asmcall.h>
  9. #endif
  10.  
  11. #include "filereq.h"
  12.  
  13. #include <string.h>
  14.  
  15. extern struct IClass *ButtonImgClass;
  16.  
  17. extern ULONG ASM myTextLength (ASM_REGPARAM(a1, char *,),
  18.                                ASM_REGPARAM(a0, struct TextAttr *,),
  19.                    ASM_REGPARAM(a3, UBYTE *,),
  20.                    ASM_REGPARAM(a2, struct Image *,),
  21.                    ASM_REGPARAM(d7, ULONG,));
  22.  
  23. const UWORD defaultpens[] =
  24. {
  25.     1, 0, 1, 2, 1, 3, 1, 0, 2
  26. };
  27.  
  28. /****************************************************************************************/
  29.  
  30. char KeyFromStr(char *string, char underchar)
  31. {
  32.     char c, ret = 0;
  33.     
  34.     while ((c = *string++))
  35.     {
  36.         if (c == underchar)
  37.     {
  38.         ret = ToUpper(*string);
  39.         break;
  40.     }
  41.     }
  42.     
  43.     return ret;
  44. }
  45.  
  46. /****************************************************************************************/
  47.  
  48. ULONG myTextLength(char *str, struct TextAttr *attr, UBYTE *underscore,
  49.            struct Image *im, ULONG do_lod)
  50. {
  51.     struct RastPort temprp;
  52.     struct TextFont *font = NULL;
  53.     WORD   pixellen = 0;
  54.     
  55.     InitRastPort(&temprp);
  56.     
  57.     if (attr)
  58.     {
  59.         font = OpenFont(attr);
  60.     if (font)
  61.     {
  62.         SetFont(&temprp, font);
  63.     } 
  64.     else
  65.     {
  66.        SetFont(&temprp, GfxBase->DefaultFont);
  67.     }
  68.     }
  69.     else
  70.     {
  71.         SetFont(&temprp, GfxBase->DefaultFont);
  72.     }
  73.     
  74.     if (str)
  75.     {
  76.         struct LocalObjData     *lod = (struct LocalObjData *)0xDEADBEAF;    
  77.     char            c, *str2;
  78.         WORD             len = strlen(str), underoff;
  79.  
  80.         if (do_lod)
  81.     {
  82.         im->PlaneOnOff = 0;
  83.         
  84.         lod = INST_DATA(ButtonImgClass, im);
  85.         lod->lod_UnderOff = len;
  86.         
  87.         im->Height = temprp.TxHeight;
  88.     }
  89.     
  90.         pixellen = TextLength(&temprp, str, len);
  91.     
  92.     str2 = str;
  93.     
  94.     underoff = -1;
  95.     
  96.     while((c = *str2++))
  97.     {
  98.         underoff++;
  99.         
  100.         if (c == *underscore)
  101.         {
  102.         pixellen -= TextLength(&temprp, underscore, 1);
  103.         
  104.             if (do_lod)
  105.         {
  106.             im->PlaneOnOff = *str2;             /* store code of underscored key */
  107.             lod->lod_UnderOff = underoff;        /* store offset of underscore */
  108.             lod->lod_RestLen = len - underoff - 1;    /* store len of remaining string */
  109.             lod->lod_UnderY = temprp.TxBaseline + 2;    /* Y position of underscore */
  110.  
  111.             lod->lod_UnderWidth = TextLength(&temprp, str2, 1);
  112.         }
  113.             break;
  114.         }
  115.     }
  116.  
  117.     } /* if (str) */
  118.     
  119. #ifdef _AROS
  120.     DeinitRastPort(&temprp);
  121. #endif
  122.  
  123.     if (font) CloseFont(font);
  124.  
  125.     return pixellen;
  126. }
  127.  
  128. /****************************************************************************************/
  129.  
  130. #define imsg ((struct impDraw *)msg)
  131.  
  132. #ifdef _AROS
  133. AROS_UFH3(IPTR, myBoopsiDispatch,
  134.       AROS_UFHA(Class *, cl, A0),
  135.       AROS_UFHA(struct Image *, im, A2),
  136.       AROS_UFHA(Msg, msg, A1))
  137. #else
  138. IPTR myBoopsiDispatch(REGPARAM(a0, Class *, cl),
  139.                       REGPARAM(a2, struct Image *, im),
  140.               REGPARAM(a1, Msg, msg))
  141. #endif
  142. {
  143.     struct LocalObjData *data;
  144.     struct TextFont    *font, *oldfont;
  145.     struct RastPort    *rp;
  146.     struct Gadget     *gad;
  147.     UWORD         *pens;
  148.     WORD         xpos, ypos;
  149.     IPTR         retval = 0;
  150.     
  151.     switch(msg->MethodID)
  152.     {
  153.         case OM_NEW:
  154.          retval = DoSuperMethodA(cl, (Object *)im, msg);
  155.          if (retval)
  156.         {
  157.             UBYTE underscorechar;
  158.         
  159.              im = (struct Image *)retval;
  160.         
  161.         data = INST_DATA(cl, im);
  162.         data->lod_IData = *(struct InitData *)im->ImageData;
  163.         
  164.         underscorechar = (UBYTE)data->lod_IData.idata_Underscore;
  165.             
  166.          im->Width = myTextLength(data->lod_IData.idata_Label,
  167.                      data->lod_IData.idata_TextAttr,
  168.                      &underscorechar,
  169.                      im,
  170.                      cl->cl_InstOffset    /* for C Routines its not really more than a flag */
  171.                      );
  172.         
  173.         /* Calculate text position if we have a gadget */
  174.                       
  175.         if ((gad = data->lod_IData.idata_Gadget))
  176.         {
  177.             im->LeftEdge = (gad->Width - im->Width) / 2;
  178.             im->TopEdge = (gad->Height - im->Height) / 2;
  179.             
  180.         }
  181.         
  182.         im->ImageData = (UWORD *)im;
  183.         *(ULONG *)&im->Width = BUTTON_MAGIC_LONGWORD;
  184.         }
  185.         break;
  186.     
  187.     case IM_DRAW:
  188.         data = INST_DATA(cl, im);
  189.         
  190.         rp = imsg->imp_RPort;
  191.         
  192.         SetDrMd(rp, JAM1);
  193.         oldfont = rp->Font;
  194.         font = OpenFont(data->lod_IData.idata_TextAttr);
  195.  
  196.         if (font)
  197.             SetFont(rp, font);
  198.         else
  199.             SetFont(rp, GfxBase->DefaultFont);
  200.     
  201.         SetSoftStyle(rp, data->lod_IData.idata_TextAttr->ta_Style, data->lod_IData.idata_TextAttr->ta_Style);
  202.         
  203.         xpos = imsg->imp_Offset.X;
  204.         ypos = imsg->imp_Offset.Y;
  205.         
  206.         pens = imsg->imp_DrInfo->dri_Pens;
  207.         if (!pens) pens = (UWORD *)defaultpens;
  208.         
  209.         if ((gad = data->lod_IData.idata_Gadget))
  210.         {
  211.             xpos = gad->LeftEdge;
  212.         ypos = gad->TopEdge;
  213.         
  214.             SetAPen(rp, pens[(imsg->imp_State == IDS_SELECTED) ? FILLPEN : BACKGROUNDPEN]);
  215.  
  216.         RectFill(rp, xpos,
  217.                  ypos,
  218.                  gad->LeftEdge + gad->Width - 1,
  219.                  gad->TopEdge + gad->Height -1 );
  220.         im->PlanePick = (imsg->imp_State == IDS_SELECTED) ? FILLTEXTPEN : TEXTPEN; 
  221.         }
  222.         
  223.         xpos += im->LeftEdge;
  224.         ypos += im->TopEdge;
  225.  
  226.         SetAPen(rp, pens[im->PlanePick]);
  227.         
  228.            Move(rp, xpos, ypos + rp->TxBaseline);
  229.         
  230.         /* Draw first part of text (entire text if no underscore */
  231.         
  232.         if (data->lod_UnderOff)
  233.             Text(rp, data->lod_IData.idata_Label, data->lod_UnderOff);
  234.     
  235.         /* Is there an underscore in text */
  236.         
  237.         if (im->PlaneOnOff)
  238.         {
  239.             /* First draw rest of text */
  240.         
  241.         xpos = rp->cp_x;
  242.         
  243.         Text(rp, data->lod_IData.idata_Label + data->lod_UnderOff + 1, data->lod_RestLen);
  244.         
  245.         /* Draw underscore */
  246.         
  247.         Move(rp, xpos, ypos + data->lod_UnderY);
  248.         Draw(rp, xpos + data->lod_UnderWidth - 1, ypos + data->lod_UnderY);        
  249.         }
  250.         
  251.         if (gad)
  252.         {
  253.             struct TagItem beveltags[] =
  254.         {
  255.             {GT_VisualInfo    , (IPTR)data->lod_IData.idata_VisualInfo    },
  256.             {GTBB_Recessed      , TRUE                                            },
  257.             {TAG_DONE                                }
  258.         };
  259.         
  260.         if (imsg->imp_State != IDS_SELECTED) beveltags[1].ti_Tag = TAG_IGNORE;
  261.         
  262.             /* draw bevelbox around gadget container (recessed if selected) */
  263.  
  264.         DrawBevelBoxA(rp,
  265.                   gad->LeftEdge,
  266.                   gad->TopEdge,
  267.                   gad->Width,
  268.                   gad->Height,
  269.                   beveltags);
  270.         }
  271.         
  272.         SetFont(rp, oldfont);
  273.         if (font) CloseFont(font);
  274.         break;
  275.         
  276.     default:
  277.         retval = DoSuperMethodA(cl, (Object *)im, msg);
  278.         break;
  279.         
  280.     } /* switch(msg->MethodID) */
  281.     
  282.     return retval;
  283.     
  284. }
  285.  
  286. /****************************************************************************************/
  287. /****************************************************************************************/
  288.